#pragma once
#include "afn0dbase.h"

#pragma pack(1)
class ActiveAfn0df2 : public Afn0dfbase
{
public:
	CommonA20 _time;
public:
	inline ActiveAfn0df2(uint16_t pn = 0)
	:Afn0dfbase(2, pn)
	{
	}
	inline uint16_t length(void)const
	{
		return sizeof(*this);
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		m["Fn"]   = Fn();
		m["Pn"]   = Pn();
		m["_time"] = _time.toStringU();
		return m;
	}
	inline bool set(const Mapss& m)
	{
		if(!m.have("Pn")
		|| !m.have("_time")
		|| !m.equal("Fn", Fn()))
		{
			return false;
		}
		int pn = 0;
		Mapss M = m;
		Pn(M["Pn"].toint());
		M["_time"] >> _time;
		return true;
	}
};

class PasiveAfn0df2 : public Afn0dfbase
{
public:
	enum{RATE_MIN = 1, RATE_MAX = 12};
public:
	CommonA20 _froztime;
	CommonA15 _copytime;
	uint8_t   _rate;
	CommonA14 _rvsttap;			//reverseTotalActivePower;
	CommonA14 _rvsap[RATE_MAX];	//reverseActivePower[RATE_MAX];
	CommonA11 _rvsttrp;			//reverseTotalReactivePower;
	CommonA11 _rvsrp[RATE_MAX];	//reverseReactivePower[RATE_MAX];
	CommonA11 _ps2ttrp;			//phase2TotalReactivePower;
	CommonA11 _ps2rp[RATE_MAX];	//phase2ReactivePower[RATE_MAX];
	CommonA11 _ps3ttrp;			//phase3TotalReactivePower;
	CommonA11 _ps3rp[RATE_MAX];	//phase3ReactivePower[RATE_MAX];
public:
	PasiveAfn0df2(uint16_t pn = 0)
	:Afn0dfbase(2, pn),_rate(RATE_MAX)
	{
	}
	inline uint16_t length(void)const
	{
		if(_rate >= RATE_MIN && _rate <= RATE_MAX)
		{
			return sizeof(PasiveAfn0df2)
				 - (RATE_MAX - _rate) * sizeof(CommonA14)
				 - (RATE_MAX - _rate) * sizeof(CommonA11)
				 - (RATE_MAX - _rate) * sizeof(CommonA11)
				 - (RATE_MAX - _rate) * sizeof(CommonA11)
				 ;
		}
	}
	inline void rate(uint8_t r)
	{
		if(r >= RATE_MIN && r <= RATE_MAX)
		{
			_rate = r;
		}
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		return m;
	}
	inline bool set(const Mapss& m)
	{
		if(!m.have("Pn")
		|| !m.equal("Fn", Fn())
		|| !m.have("_froztime")
		|| !m.have("_copytime")
		|| !m.have("_rate")	  
		|| !m.have("_rvsttap")  
		|| !m.have("_rvsttrp")  
		|| !m.have("_ps2ttrp") 
		|| !m.have("_ps3ttrp") )
		{
			cout << __func__ << "," << __LINE__ << endl;
			return false;
		}
		Mapss M = m;

		Pn(M["Pn"].toint());
		M["_froztime"] >> _froztime;
		M["_copytime"] >> _copytime;
		M["_rate"	 ] >> _rate;
		M["_rvsttap" ] >> _rvsttap;
		M["_rvsttrp" ] >> _rvsttrp;	
		M["_ps2ttrp" ] >> _ps2ttrp;
		M["_ps3ttrp" ] >> _ps3ttrp;
		for(int i = 0; i < _rate; i++)
		{
			sstream rvsap;
			sstream rvsrp;
			sstream ps2rp;
			sstream ps3rp;

			cout <<  i << endl;
			rvsap << "_rvsap_" << i;
			rvsrp << "_rvsrp_" << i;
			ps2rp << "_ps2rp_" << i;
			ps3rp << "_ps3rp_" << i;

			M[rvsap.str()] >> _rvsap[i];	
			M[rvsrp.str()] >> _rvsrp[i];
			M[ps2rp.str()] >> _ps2rp[i];
			M[ps3rp.str()] >> _ps3rp[i];
		}
		cout << hexBySize(_froztime) << endl;
		cout << hexBySize(_copytime) << endl;
		return true;
	}
};
#pragma pack(0)
